package com.zghhost.codedemo;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.zghhost.codedemo.util.IOUtil;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/13 00:51
 */
public class RequestFor {
    static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("demo-pool-%d").build();
    static ExecutorService pool = new ThreadPoolExecutor(1024, 1024,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args)throws Exception{
        for(int i=0,ic=100;i<ic;i++){
            pool.execute(()->{
                try{
                    while (true){
//                        URL url = new URL("http://lzadmin.mt.ex.piaodaren.com/general_api/tcss/index#/");
                        URL url = new URL("http://localhost:9090/");
                        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                        InputStream is = null;
                        try{
                            is = conn.getInputStream();
                            String s = IOUtil.is2str(is);
//                            System.out.println(s);
                        }catch (Exception e){
                        }finally {
                            if(is != null){
                                is.close();
                            }
                        }

                        InputStream eis = null;
                        try{
                            eis = conn.getErrorStream();
                            String s = IOUtil.is2str(eis);
//                            System.out.println(s);
                        }catch (Exception e){
                        }finally {
                            if(eis != null){
                                eis.close();
                            }
                        }
                        conn.disconnect();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
        }
    }
}
